<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/19
 * Time: 11:48
 */

namespace app\zucai\home;

use app\index\controller\Home;
use think\Db;

/**
 * Class Manage
 * @package app\zucai\home
 * 足彩赛事信息
 */
class Manage extends Home
{
    //彩种确认
    public function caizhong()
    {
        if ($this->request->isPost()) {
            if ($this->request->post()) {  //传值为cid
                $post = $this->request->post();

                $con = Db::name('caizhong')
                    ->where('id', $post['cid'])
                    ->find();
                if ($con) {
                    return returnOk($con);
                } else {
                    return returnError('暂无数据！');
                }
            } else {
                return returnError('POST传值错误!');
            }
        } else {
            return returnError('请使用POST方式请求');
        }
    }

//赛事筛选
    public function index()
    {
        $con = Db::name('play')
            ->field('distinct(title)')
            ->select();
        $arr = array();
        for ($i = 0; $i < count($con); $i++) {

            $arr[$i] = $con[$i]['title'];
        }
        if ($arr) {
            return returnOk($arr);
        } else {
            return returnError('暂无数据！');
        }
    }

    //赛事详情遍历
    public function lst()
    {
        if ($this->request->post()) {  //传值为赛事筛选查询 $title --数组
            $post = $this->request->post();
            $con = Db::name('play')
                ->field('id,cid,title,ftitle,team,team2,rqcls,teamid,team2id,firsttime,shenfu,shenfu2,bifen,all,banall,first_date')
                ->where('title', 'in', $post['title'])
                ->where('isopen', 1)
                ->where('status', 0)
                ->order('ftitle,firsttime')
                ->select();

        } else {
            $con = Db::name('play')
                ->field('id,cid,title,ftitle,team,team2,rqcls,teamid,team2id,firsttime,shenfu,shenfu2,bifen,all,banall,first_date')
                ->where('isopen', 1)
                ->where('status', 0)
                ->order('firsttime,ftitle')
                ->select();
        }

        if ($con) {

			foreach($con as $k=>$v){
				if(date('Y-m-d',time())<date('Y-m-d',strtotime($v['firsttime']))&&date('Y-m-d',time()+86400)==date('Y-m-d',strtotime($v['firsttime']))){
					if(date('w',strtotime($v['firsttime']))==1||date('w',strtotime($v['firsttime']))==0){
						if(date('H',strtotime($v['firsttime']))>=1&&date('H',strtotime($v['firsttime']))<10){
						$con[$k]['firsttime']=date('Y-m-d',time()+86400)." 0:30:00";
					}else{
						$con[$k]['firsttime']=date('Y-m-d H:i:s',strtotime($v['firsttime'])-1800);
					}
					}
					else{
						if(date('H',strtotime($v['firsttime']))>=0&&date('H',strtotime($v['firsttime']))<10){
						$con[$k]['firsttime']=date('Y-m-d',time())." 23:30:00";
					}else{
						$con[$k]['firsttime']=date('Y-m-d H:i:s',strtotime($v['firsttime'])-1800);
					}
					}
				}
				else if(date('Y-m-d',time())==date('Y-m-d',strtotime($v['firsttime']))){
					if(date('w',strtotime($v['firsttime']))==1||date('w',strtotime($v['firsttime']))==0){
						if(date('H',strtotime($v['firsttime']))>=1&&date('H',strtotime($v['firsttime']))<10){
							unset($con[$k]);
						}
						else{
							$con[$k]['firsttime']=date('Y-m-d H:i:s',strtotime($v['firsttime'])-1800);
						}
					}else{
						if(date('H',strtotime($v['firsttime']))>=0&&date('H',strtotime($v['firsttime']))<10){
							unset($con[$k]);
						}else{
							$con[$k]['firsttime']=date('Y-m-d H:i:s',strtotime($v['firsttime'])-1800);
						}
					}
				}
				else{
					$con[$k]['firsttime']=date('Y-m-d H:i:s',strtotime($v['firsttime'])-1800);
				}
			}
			
			foreach($con as $k=>$v){
				if(strtotime($v['firsttime'])<=time()) unset($con[$k]);
			}
			foreach($con as $k=>$v){
				$banall=explode(',',$v['banall']);
				foreach($banall as $k2=>$v2){
					if($v2==='') $banall[$k2]='-';
				}
				$con[$k]['banall']=implode(',',$banall);
				
				$all=explode(',',$v['all']);
				foreach($all as $k2=>$v2){
					if($v2==='') $all[$k2]='-';
				}
				$con[$k]['all']=implode(',',$all);
				
				$bifen=explode(',',$v['bifen']);
				foreach($bifen as $k2=>$v2){
					if($v2==='') $bifen[$k2]='-';
				}
				$con[$k]['bifen']=implode(',',$bifen);
			}
			
            $con2=array();
            foreach ($con as $k => $v) {
				$v['first_date']=date('Y-m-d',strtotime($v['firsttime']));
                $con2[date('Y-m-d',strtotime($v['firsttime']))][]=$v;
            }

            foreach ($con2 as $k=>$v) {
                $con3[]=$v;
            }

            return returnOk($con3);
        } else {
            return returnError('暂无数据！');
        }
    }
	
	
	 /**
     * gxq <know1111@vip.qq.com>
     * 昨日比分
     */
    public function score()
    {

        $play=Db::name('play')
            ->alias('a')
            ->field('ftitle,title,firsttime,rqcls,team,team2,score,teamid,team2id,status')
            ->join('kaijiang b','a.id=b.play_id','left')
            ->order('firsttime desc,ftitle')
            ->where('status','in',[1,2])
            ->where('DATEDIFF(firsttime,NOW())=-1')
            ->select();

        foreach ($play as $k=>$v) {
            $play[$k]['firsttime']=date('H:i',strtotime($v['firsttime']));
        }

        return returnOk($play);
    }
}